﻿Imports System.Windows.Forms
Imports DNP.GestionComercial.BE
Imports DNP.GestionComercial.BL
Imports DNP.GestionComercial.Util
Imports DNP.GestionComercial.FormBase

Public Class frmBusProveedor
    Inherits DNP.GestionComercial.FormBase.frmBusqueda

#Region "Declaracion de variables"

    Dim oMsg As New sisMensaje
    Dim _codigo As String = ""
    Dim _descripcion As String = ""
    Dim _frm As New Windows.Forms.Form

#End Region

#Region "Iniciar Página"

    Public Sub New(ByRef codigo As String, ByRef descripcion As String, ByRef frm As Windows.Forms.Form)

        _codigo = codigo
        _descripcion = descripcion
        _frm = frm
        InitializeComponent()

    End Sub

    Public Sub New(ByVal frm As Windows.Forms.Form)

        InitializeComponent()

    End Sub

    Private Sub frmBusProveedor_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If Asc(e.KeyValue).ToString = Asc(Keys.Escape).ToString Then
            Me.Close()
        End If
    End Sub

    Private Sub frmBusProveedor_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        CargarFormulario()
    End Sub

#End Region

#Region "Funciones"

    Private Sub FormatGrid()

        grdLista.AutoGenerateColumns = False
        grdLista.Columns.Clear()
        grdLista.Columns.Add("NPRO_CODIGO", "Código")
        grdLista.Columns.Add("CPRO_RUC", "RUC")
        grdLista.Columns.Add("CPRO_RAZONSOCIAL", "Razon Social")

        grdLista.Columns(0).DataPropertyName = "NPRO_CODIGO"
        grdLista.Columns(1).DataPropertyName = "CPRO_RUC"
        grdLista.Columns(2).DataPropertyName = "CPRO_RAZONSOCIAL"
        
        grdLista.Columns(0).Width = 60
        grdLista.Columns(1).Width = 100
        grdLista.Columns(2).Width = 400
        
        grdLista.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdLista.Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        grdLista.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdLista.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        grdLista.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
        grdLista.Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        grdLista.Columns(0).Visible = False

    End Sub

    Private Sub CargarFormulario()

        lblTitulo.Text = "Búsqueda de Proveedores"
        Listar()

    End Sub

    Private Sub Listar()

        Dim oRespuesta As New RespuestaBE
        Dim oParametro As New PARAMETROBE

        oParametro.PARAMETRO01 = txtEntidad.Text
        oRespuesta = New ProveedorBL().ListarTodos(oParametro)

        If oRespuesta.Respuesta Then
            FormatGrid()
            grdLista.DataSource = oRespuesta.ValorRertono
            grdLista.Refresh()
        Else
            oMsg.showMsgWarning(oRespuesta.Mensaje)
            MsgBox(oRespuesta.Mensaje)
        End If

    End Sub

#End Region

    Public Overrides Sub btnCerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Close()
    End Sub

    Public Overrides Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (vRespuesta.PARAMETRO01 Is Nothing) Then
            MsgBox("Seleccione un Proveedor")
        Else
            DialogResult = Windows.Forms.DialogResult.OK
            Me.Hide()
        End If
    End Sub

    Public Overrides Sub txtEntidad_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Listar()
    End Sub

    Public Overrides Sub grdLista_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles grdLista.KeyDown

        If Asc(e.KeyValue).ToString = Asc(Keys.Enter).ToString Then

            Dim row As Integer = grdLista.CurrentRow.Index

            Dim cell As DataGridViewCell
            cell = grdLista.Rows(grdLista.CurrentRow.Index).Cells(grdLista.CurrentCell.ColumnIndex)

            If cell.Selected Then
                vRespuesta.PARAMETRO01 = grdLista.Rows(row).Cells(1).Value.ToString().Trim() ' ruc
                vRespuesta.PARAMETRO02 = grdLista.Rows(row).Cells(2).Value.ToString().Trim() ' razon social
            End If

            If (vRespuesta.PARAMETRO01 Is Nothing) Then
                MsgBox("Seleccione un Proveedor")
            Else
                DialogResult = Windows.Forms.DialogResult.OK
                Me.Hide()
            End If

        End If

    End Sub

End Class